package com.xingkeduo.controller.cms.marketing;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.model.param.MarketingParam.MarketingServiceTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingTypeEnum;
import com.xingkeduo.model.param.MarketingReceiveHistoryParam;
import com.xingkeduo.model.param.MarketingReceiveHistoryParam.MarketingReceiveSendTypeEnum;
import com.xingkeduo.model.param.MarketingReceiveHistoryParam.MarketingReceiveStatusEnum;
import com.xingkeduo.service.cms.marketing.MarketingReceiveService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.StringUtils;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
 * @ClassName: MarketingReceiveCmsController
 * @Description: 营销发放的领取记录
 * @author lixin
 * @date Apr 24, 2019 1:54:40 PM
 *
 */
@RestController
@RequestMapping(Constant.CMS_URL_PREFIX + "/receive")
public class MarketingReceiveCmsController {
	Logger logger =LoggerFactory.getLogger(MarketingReceiveCmsController.class);

	@Autowired
	@Qualifier(value = "marketingCouponReceiveService")
	MarketingReceiveService marketingCouponReceiveService;
	@Autowired
	@Qualifier(value = "marketingCouponBagReceiveService")
	MarketingReceiveService marketingCouponBagReceiveService;
	@Autowired
	@Qualifier(value = "marketingCardReceiveService")
	MarketingReceiveService marketingCardReceiveService;
	@Autowired
	@Qualifier(value = "marketingReceiveService")
	MarketingReceiveService marketingReceiveService;

	@RequestMapping("/getUserCouponReceiveList")
	public ResponseMessage getUserCouponReceiveList(MarketingReceiveHistoryParam couponReceiveHistoryExample) {
		couponReceiveHistoryExample.setMarketingType(MarketingTypeEnum.COUPON.getCode());
		if (StringUtils.isNotBlank(couponReceiveHistoryExample.getServiceType()) && MarketingServiceTypeEnum
				.parse(couponReceiveHistoryExample.getServiceType()).equals(MarketingServiceTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("券类型不合法", 500);
		}
		if (couponReceiveHistoryExample.getStatus() != null && MarketingReceiveStatusEnum
				.parseStatus(couponReceiveHistoryExample.getStatus()).equals(MarketingReceiveStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("券状态不合法", 500);
		}
		if (couponReceiveHistoryExample.getSendType() != null && MarketingReceiveSendTypeEnum
				.parseStatus(couponReceiveHistoryExample.getSendType()).equals(MarketingReceiveSendTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("券领取方式不合法", 500);
		}
		return marketingCouponReceiveService.getUserMarketingReceiveList(couponReceiveHistoryExample);
	}

	@RequestMapping("/getUserCouponUseDetail")
	public ResponseMessage getUserCouponUseDetail(Long couponReceiveHistoryId) {
		return marketingCouponReceiveService.getUserMarketingUseDetail(couponReceiveHistoryId,MarketingTypeEnum.COUPON.getCode());
	}

	@RequestMapping("/auditUserCoupon")
	public ResponseMessage auditUserCoupon(Long couponReceiveHistoryId, Integer status) {
		if (MarketingReceiveStatusEnum.DISABLED.getCode() != status
				&& MarketingReceiveStatusEnum.AVAILABLE.getCode() != status) {
			return ResponseMessage.error("券状态变更不合法", 500);
		}
		return marketingCouponReceiveService.auditUserMarketing(couponReceiveHistoryId, MarketingTypeEnum.COUPON.getCode(), status);
	}

	@RequestMapping("/getUserCardReceiveList")
	public ResponseMessage getUserCardReceiveList(MarketingReceiveHistoryParam cardReceiveHistoryExample) {
		cardReceiveHistoryExample.setMarketingType(MarketingTypeEnum.CARD.getCode());
		if (StringUtils.isNotBlank(cardReceiveHistoryExample.getServiceType()) && MarketingServiceTypeEnum
				.parse(cardReceiveHistoryExample.getServiceType()).equals(MarketingServiceTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("卡类型不合法", 500);
		}
		if (cardReceiveHistoryExample.getStatus() != null && MarketingReceiveStatusEnum
				.parseStatus(cardReceiveHistoryExample.getStatus()).equals(MarketingReceiveStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("卡状态不合法", 500);
		}
		if (cardReceiveHistoryExample.getSendType() != null && MarketingReceiveSendTypeEnum
				.parseStatus(cardReceiveHistoryExample.getSendType()).equals(MarketingReceiveSendTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("卡领取方式不合法", 500);
		}
		return marketingCardReceiveService.getUserMarketingReceiveList(cardReceiveHistoryExample);
	}

	@RequestMapping("/getUserCardUseDetail")
	public ResponseMessage getUserCardUseDetail(Long cardReceiveHistoryId) {
		return marketingCardReceiveService.getUserMarketingUseDetail(cardReceiveHistoryId, MarketingTypeEnum.CARD.getCode());
	}

	@RequestMapping("/auditUserCard")
	public ResponseMessage auditUserCard(Long cardReceiveHistoryId, Integer status) {
		if (MarketingReceiveStatusEnum.DISABLED.getCode() != status
				&& MarketingReceiveStatusEnum.AVAILABLE.getCode() != status) {
			return ResponseMessage.error("券状态变更不合法", 500);
		}
		return marketingCardReceiveService.auditUserMarketing(cardReceiveHistoryId, MarketingTypeEnum.CARD.getCode(), status);
	}

	@RequestMapping("/getUserCouponBagReceiveList")
	public ResponseMessage getUserCouponBagReceiveList(MarketingReceiveHistoryParam couponBagReceiveHistoryExample) {
		couponBagReceiveHistoryExample.setMarketingType(MarketingTypeEnum.COUPON_BAG.getCode());
		if (StringUtils.isNotBlank(couponBagReceiveHistoryExample.getServiceType()) && MarketingServiceTypeEnum
				.parse(couponBagReceiveHistoryExample.getServiceType()).equals(MarketingServiceTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("卡类型不合法", 500);
		}
		if (couponBagReceiveHistoryExample.getStatus() != null && MarketingReceiveStatusEnum
				.parseStatus(couponBagReceiveHistoryExample.getStatus()).equals(MarketingReceiveStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("卡状态不合法", 500);
		}
		if (couponBagReceiveHistoryExample.getSendType() != null
				&& MarketingReceiveSendTypeEnum.parseStatus(couponBagReceiveHistoryExample.getSendType())
						.equals(MarketingReceiveSendTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("卡领取方式不合法", 500);
		}
		return marketingCouponBagReceiveService.getUserMarketingReceiveList(couponBagReceiveHistoryExample);
	}

	@RequestMapping("/getUserCouponBagUseDetail")
	public ResponseMessage getUserCouponBagUseDetail(Long couponBagReceiveHistoryId, Integer marketingType) {
		return marketingCouponBagReceiveService.getUserMarketingUseDetail(couponBagReceiveHistoryId, marketingType);
	}

	@RequestMapping("/getMarketingBagUseHisory")
	public ResponseMessage getMarketingBagUseHisory(Long marketingReceiveHistoryId, Integer marketingType) {
		return marketingCouponBagReceiveService.getMarketingBagUseHisory(marketingReceiveHistoryId, marketingType);
	}
	
	@RequestMapping("/getUserCouponUseHisory")
	public ResponseMessage getUserCouponUseHisory(Long userId,Integer pageNum,Integer pageSize) {
		if (userId == null) {
			return ResponseMessage.error("用户ID参数缺失", 500);
		}
		if(pageNum==null) {
			pageNum=0;
		}
		if(pageSize==null) {
			pageSize=10;
		}
		return marketingCouponReceiveService.getUserMarketingCouponUseHisory(userId,pageNum,pageSize);
	}
	@RequestMapping("/getUserCardUseHisory")
	public ResponseMessage getUserCardUseHisory(Long userId,Integer pageNum,Integer pageSize) {
		if (userId == null) {
			return ResponseMessage.error("用户ID参数缺失", 500);
		}
		if(pageNum==null) {
			pageNum=0;
		}
		if(pageSize==null) {
			pageSize=10;
		}
		return marketingCardReceiveService.getUserMarketingCardUseHisory(userId,pageNum,pageSize);
	}

	@RequestMapping("/auditUserCouponBag")
	public ResponseMessage auditUserCouponBag(Long couponBagReceiveHistoryId,Integer status) {
		if (couponBagReceiveHistoryId == null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		if (status == null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		if(status.intValue()!=MarketingReceiveStatusEnum.AVAILABLE.getCode()
					&& status.intValue()!=MarketingReceiveStatusEnum.DISABLED.getCode()) {
			return ResponseMessage.error("券包更新状态错误", 500);
		}
		return marketingCouponBagReceiveService.auditUserMarketing(couponBagReceiveHistoryId, null, status);
	}

	@RequestMapping("/getReceiveLink")
	public ResponseMessage getReceiveLink(Long marketingId, Integer marketingType) {
		return marketingReceiveService.getReceiveLink(marketingId, marketingType);
	}
}
